package com.woniuxy.background.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.background.advice.SysLog;
import com.woniuxy.background.common.RestResult;
import com.woniuxy.background.pojo.*;
import com.woniuxy.background.service.*;
import com.woniuxy.background.vo.OrderVO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderitemService orderitemService;

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private UsersService usersService;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private GoodsTypeService goodsTypeService;

    @RequestMapping("/list")
    @RequiresPermissions("order:order:list")
    public RestResult list(@RequestParam(value = "page",defaultValue = "1",required = false) Integer currentPage,
                           @RequestParam(value = "limit",defaultValue = "5",required = false) Integer pageSize){
        PageInfo pageInfo = orderitemService.selectAllByPage(currentPage,pageSize);
        return RestResult.success(pageInfo);
    }
    @PostMapping("/updOrder")
    @ResponseBody
    @SysLog(value = "发货操作")
    @RequiresPermissions("order:good:send")
    public RestResult updOrder(Orders orders){
        try {
            if (orders.getStatus().equals("waitDelivery")){
                orders.setStatus("waitConfirm");
                orders.setDeliverydate(new Date());
                ordersService.updateByPrimaryKeySelective(orders);
            }
        }catch (Exception e){
            e.printStackTrace();
            RestResult.failed("更新失败！");
        }
        return RestResult.success("更新成功！");
    }

    @RequestMapping("/orders")
    @ResponseBody
    @RequiresPermissions("order:order:list")
    public RestResult orders(@RequestParam(value = "page",defaultValue = "1",required = false) Integer currentPage,
                             @RequestParam(value = "limit",defaultValue = "5",required = false) Integer pageSize, OrderVO vo){
        List<Orders> orders = ordersService.selectAllByPage(currentPage,pageSize,vo);
        PageInfo pageInfo = new PageInfo(orders);
        return RestResult.success(pageInfo);
    }

    @GetMapping("/checkUser/{id}")
    @RequiresPermissions("order:order:list")
    public ModelAndView checkUser(@PathVariable("id")int id, ModelAndView modelAndView){
        Orderitem orderitem = orderitemService.selectByOid(id);
        User user = usersService.findById(orderitem.getUserId());
        modelAndView.addObject("user",user);
        modelAndView.setViewName("users/checkUser");
        return modelAndView;
    }
    @GetMapping("/checkGoods/{id}")
    @RequiresPermissions("order:order:list")
    public ModelAndView checkGoods(@PathVariable("id")int id, ModelAndView modelAndView){
        Orderitem orderitem = orderitemService.selectByOid(id);
        Goods goods = goodsService.selectByPrimaryKey(orderitem.getGoodsId());
        List<GoodsType> goodsTypes = goodsTypeService.selectAll();
        modelAndView.addObject("goodsTypes",goodsTypes);
        modelAndView.addObject("goods",goods);
        modelAndView.setViewName("order/goodInfo");
        return modelAndView;
    }



    @DeleteMapping("/delete/{id}")
    @ResponseBody
    @SysLog(value = "删除订单")
    @RequiresPermissions("order:order:delete")
    public RestResult deleteOrder(@PathVariable("id")int id){
        try {
            Orderitem orderitem = orderitemService.selectByOid(id);

            ordersService.deleteByPrimaryKey(id);
            orderitemService.deleteByPrimaryKey(orderitem.getId());
        }catch (Exception e){
            e.printStackTrace();
            return RestResult.failed("删除失败！");
        }
        return RestResult.success("删除成功！");
    }
}
